package com.jstyle.jclife.view;



import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.Typeface;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.View;
import android.view.animation.DecelerateInterpolator;

import com.jstyle.jclife.R;
import com.jstyle.jclife.utils.ScreenUtils;

import java.text.NumberFormat;
import java.util.Locale;

/**
 * 圆形进度条
 * 
 * @author Administrator
 *
 */
public class ActivityHistoryCircleProgress extends View {
	private String titleText;
	private  int circleRadiusSmall;
	private  float circleRadiusCenter;
	private Paint progressPaint;//进度条画笔

	private int strokeWidth;

	private RectF rectF;
	private float endAngel = 0;
	float startAngle=90;

	private Paint ballPaint;//小球画笔
	private int textSize,smallTextSize,normalTextSize;
	private Paint bgCiclePaint;//背景圆画笔
	Paint textPaint;


	private int defaultWidth;
	private int defaultHeight;
	private float circleRadius;
	private Bitmap stepBitmap;
	private String progressString="0%";
	private NumberFormat numberFormat;
	private String defaultTitleText;
	private int centerTextSize;
	private int padding;
	private int progressColor;


	public ActivityHistoryCircleProgress(Context context, AttributeSet attrs) {
		super(context, attrs);
		// TODO Auto-generated constructor stub
		defaultTitleText=getContext().getString(R.string.Steps);
		TypedArray array=context.obtainStyledAttributes(attrs,R.styleable.mCicleProgress);
		 titleText=array.getString(R.styleable.mCicleProgress_titleText);
		float textSizeDimension=  array.getInt(R.styleable.mCicleProgress_centerTextSize,24);
		if(TextUtils.isEmpty(titleText))titleText=defaultTitleText;
		circleRadius =ScreenUtils.dip2px(context, 50);
		strokeWidth = ScreenUtils.dip2px(context, 6);
		circleRadiusSmall = ScreenUtils.dip2px(context, 6);
		circleRadiusCenter = ScreenUtils.dip2px(context, 51);
		defaultHeight = ScreenUtils.dip2px(context, 150);
		defaultWidth = ScreenUtils.dip2px(context, 150);
		textSize = ScreenUtils.dip2px(context, textSizeDimension);
		normalTextSize = ScreenUtils.dip2px(context, 15);
		smallTextSize = ScreenUtils.dip2px(context, 12);
		padding=ScreenUtils.dip2px(context, 15);
		progressColor=array.getColor(R.styleable.mCicleProgress_progressColor,getResources().getColor(
				R.color.progress_color));
	//	stepBitmap= BitmapFactory.decodeResource(getResources(),R.drawable.circle_step);
		init();
	}

	private void init() {
		// TODO Auto-generated method stub
		numberFormat=NumberFormat.getPercentInstance(Locale.ENGLISH);
		numberFormat.setGroupingUsed(false);
		numberFormat.setMaximumFractionDigits(0);
		//numberFormat.setMinimumFractionDigits(0);
		rectF = new RectF();
		progressPaint = new Paint();
		progressPaint.setColor(progressColor);
		progressPaint.setStyle(Paint.Style.STROKE);
		progressPaint.setAntiAlias(true);
		progressPaint.setStrokeWidth(strokeWidth);
		progressPaint.setStrokeCap(Paint.Cap.ROUND);



		bgCiclePaint = new Paint();
		bgCiclePaint.setColor(Color.parseColor("#4f5056"));
		bgCiclePaint.setAntiAlias(true);
		bgCiclePaint.setStyle(Paint.Style.STROKE);
		bgCiclePaint.setStrokeWidth(strokeWidth);
		bgCiclePaint.setStrokeCap(Paint.Cap.ROUND);

		ballPaint=new Paint();
		ballPaint.setColor(Color.WHITE);
		ballPaint.setAntiAlias(true);

		textPaint=new Paint();
		textPaint.setColor(Color.WHITE);
		textPaint.setAntiAlias(true);
		Typeface mFace = Typeface.createFromAsset(getContext().getAssets(),"Brandon_reg.otf");
		textPaint.setTypeface(mFace);
		textPaint.setTextSize(textSize);
	}

	private String topTxt = "步数";
	private String bottomTxt = "0";

	@Override
	protected void onDraw(Canvas canvas) {
		// TODO Auto-generated method stub
		super.onDraw(canvas);
		rectF.set(0+strokeWidth, 0+strokeWidth, getWidth()-strokeWidth, getHeight()-strokeWidth);
		drawBgArc(canvas);
		drawProgress(canvas);
		drawCenterCicle(canvas);
		drawCircle(canvas);
		drawText(canvas);

	}

	private static final String TAG = "ActivityHistoryCirclePr";
	private void drawCenterCicle(Canvas canvas) {
		progressPaint.setStyle(Paint.Style.FILL);

		canvas.drawCircle(getWidth()/2,getHeight()/2,circleRadiusCenter,progressPaint);

	}


	private void drawText(Canvas canvas) {
		textPaint.setColor(Color.WHITE);
		float x=getWidth()/2;
		float y=getHeight()/2;
		textPaint.setTextSize(textSize);
		Rect rect = new Rect();
		String stepValue=bottomTxt;
		textPaint.getTextBounds(stepValue, 0, stepValue.length(), rect);
		int height = rect.height();
		int width=rect.width();
		canvas.drawText(stepValue,x-width/2,y+height/2,textPaint);
		String stepLabel=titleText;
		textPaint.setTextSize(normalTextSize);
		textPaint.getTextBounds(stepLabel, 0, stepLabel.length(), rect);
		int heightLabel = rect.height();
		int widthLabel=rect.width();
		canvas.drawText(stepLabel,x-widthLabel/2,y-circleRadiusCenter+heightLabel+padding,textPaint);

		textPaint.setTextSize(normalTextSize);
	//	Log.i(TAG, "drawText: "+progressString);
		if(Float.valueOf(progressString.split("%")[0].trim())>=100){
			textPaint.setColor(Color.WHITE);
		}
		String percentValue=progressString.split("%")[0].trim()+"%";

		textPaint.getTextBounds(percentValue, 0, percentValue.length(), rect);
		int widthPercent=rect.width();
		canvas.drawText(percentValue,x-widthPercent/2,y+circleRadiusCenter-padding,textPaint);
//		String percentLabel="%";
//		textPaint.setTextSize(smallTextSize);
//		textPaint.getTextBounds(percentLabel, 0, percentLabel.length(), rect);
//		int widthPercentLabel=rect.width();
//		canvas.drawText(percentLabel,x+widthPercent/2+widthPercentLabel/4,y+height+heightLabel,textPaint);
	}

	private void drawProgress(Canvas canvas) {
		progressPaint.setStyle(Paint.Style.STROKE);
		canvas.drawArc(rectF, startAngle, endAngel, false, progressPaint);
	}

	private void drawBgArc(Canvas canvas) {
		bgCiclePaint.setStyle(Paint.Style.STROKE);
		canvas.drawCircle(getWidth()/2,getHeight()/2,(getWidth()-strokeWidth*2)/2,bgCiclePaint);
	//	canvas.drawArc(rectF, startAngle, 270, false, bgCiclePaint);
	}

	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		// TODO Auto-generated method stub
		super.onMeasure(widthMeasureSpec, heightMeasureSpec);
		int width = MeasureSpec.getSize(widthMeasureSpec);
		int height = MeasureSpec.getSize(heightMeasureSpec);
		if (MeasureSpec.getMode(heightMeasureSpec) == MeasureSpec.AT_MOST) {
			height = defaultHeight;
		}
		if (MeasureSpec.getMode(widthMeasureSpec) == MeasureSpec.AT_MOST) {
			width = defaultWidth;
		}
		setMeasuredDimension(width, height);

	}

	private void drawCircle(Canvas canvas) {
		// TODO Auto-generated method stub
		float x = 0;
		float y = 0;
		float sin = (float) Math.sin(endAngel * Math.PI / 180);
		float cos = (float) Math.cos(endAngel * Math.PI / 180);
		float centerX=getWidth()/2;
		float centerY=getHeight()/2;
		x = sin * (centerX-strokeWidth);
		y = cos * (centerX-strokeWidth);
		canvas.drawCircle(centerX - x,   centerY+y, circleRadiusSmall, ballPaint);
	}

	public void setProgress(float progress) {
		endAngel = 360f * progress;
		progressString=numberFormat.format(progress);
		postInvalidate();
	}
	public void setProgressSleep(float progress) {
		endAngel = 360f * progress;
		if(endAngel>360)endAngel=360;
		progressString=numberFormat.format(progress);
		postInvalidate();
	}

	private float start = 0.0f;
	float lastStep;
	public void startAnim(float end,int step) {
		if (start == end&&step==lastStep)
			return;
		ObjectAnimator animator = ObjectAnimator.ofFloat(this, "progress",
				start, end);
		start = end;
		lastStep=step;
		AnimatorSet animatorSet = new AnimatorSet();
		animatorSet.setDuration(300);
		animatorSet.setInterpolator(new DecelerateInterpolator());
		animatorSet.setTarget(this);
		animatorSet.play(animator);
		animatorSet.start();

	}
	public void startAnimSleep(String text,float end) {
		if (start == end)
			return;

		this.bottomTxt=text;
		ObjectAnimator animator = ObjectAnimator.ofFloat(this, "progressSleep",
				start, end);
		start = end;

		AnimatorSet animatorSet = new AnimatorSet();
		animatorSet.setDuration(300);
		animatorSet.setInterpolator(new DecelerateInterpolator());
		animatorSet.setTarget(this);
		animatorSet.play(animator);
		animatorSet.start();

	}





	public void update(int step,float goal) {
		float progress=goal/100f;
		this.bottomTxt = String.valueOf(step);
		startAnim(progress,step);
		//postInvalidate();
	}





	
}
